/**
 * 
 */
package apa.wings.gwtCRUD.client;

import java.util.Date;

import apa.wings.gwtCRUD.shared.services.PersonService;
import apa.wings.gwtCRUD.shared.services.PersonServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * Represents the home page.
 * 
 * @author analiahojman
 * 
 */
public class PageWidget extends Composite {

	private static PageUiBinder uiBinder = GWT.create(PageUiBinder.class);

	interface PageUiBinder extends UiBinder<Widget, PageWidget> {
	}

	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);

	@UiField(provided = true)
	FlowPanel panel;

	@UiField
	Label nameLabel;
	@UiField
	TextBox nameText;
	@UiField
	Label lastNameLabel;
	@UiField
	TextBox lastNameText;
	@UiField
	Label birthDateLabel;
	@UiField
	TextBox birthDateText;
	@UiField
	Label sexLabel;
	@UiField(provided = true)
	RadioButton maleOption;
	@UiField(provided = true)
	RadioButton femaleOption;
	@UiField
	Label countryLabel;
	@UiField
	ListBox countryList;
	@UiField(provided = true)
	SuggestBox suggest;
	@UiField
	Button cancelButton;
	@UiField
	Button saveButton;

	public PageWidget() {
		super();

		panel = new FlowPanel();

		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
		oracle.add("Cat");
		oracle.add("Dog");
		oracle.add("Horse");
		oracle.add("Canary");
		suggest = new SuggestBox(oracle);

		maleOption = new RadioButton("sex", "Male");
		femaleOption = new RadioButton("sex", "Female");

		initWidget(uiBinder.createAndBindUi(this));
		this.setText();
		this.addHandlers();
	}

	private void setText() {
		nameLabel.setText("Name: ");
		lastNameLabel.setText("Last Name: ");
		birthDateLabel.setText("Birth Date: ");
		sexLabel.setText("Sex: ");
		countryLabel.setText("Country: ");
		countryList.addItem("Argentina");
		countryList.addItem("Brasil");
		countryList.addItem("Chile");
		countryList.addItem("Bolivia");

		saveButton.setText("Save");
		cancelButton.setText("Cancel");
	}

	private void addHandlers() {
		this.saveButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				try {
					personService.savePerson(new Integer(1),
							nameText.getValue(), lastNameText.getValue(),
							new Date(), "F", new AsyncCallback<Void>() {

								public void onFailure(Throwable arg0) {
									Window.alert("errrrrrrorrrrrrr");
								}

								public void onSuccess(Void arg0) {
									Window.alert("okkkkkkkkkkkkkk");

								}
							});
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}